<template>
	<view class="shop-car">
		<commonCar :list="goodsProducts" @delect="delect" @accounts="accounts" @onupdate="onupdate">
			<text></text>
		</commonCar>
	</view>
</template>

<script>
	import commonCar from '@/components/shopCar/shopCar.vue';
	import {
		loadCartList,
		delectCart
	} from "../appjs/gouwucn/gouwucnJs.js";
	import {
		loginName
	} from '../appjs/longin/login';
	export default {
		data() {
			return {
				uid: '',
				goodsProducts: [{
					name: '兰蔻京东自营官方旗舰店',
					checked: 1,
					goods: [],
					cartId: '' // 添加 cartId
				}],
			}
		},
		components: {
			commonCar
		},
		onLoad(options) {
			loginName(this.$user.username, this.$user.password).then(data => {
				data.data.forEach(item => {
					this.uid = item.userId;
				});
				this.loadGoods(); 
			});
		},
		methods: {
			delect(e) {
				let s =this
				delectCart(e).then(data => {
					if (data) {
						uni.showToast({
							title: "删除成功",
							success() {
								s.goodsProducts[0].goods=[];
								s.loadGoods();
							}
						})
					} else {
						uni.showToast({
							title: "删除失败",
							icon: 'none'
						})
						console.log("dataerty", data)
					}
				})
				console.log("delecrqqqq", e);
			},
			statistics(e) {
				console.log("cartids", e);
			},
			accounts(e) {
				console.log(e);
			},
			onupdate(e) {
				console.log(e)
				let t = this;
				t.goodsProducts[0].goods=[];
				t.loadGoods();
			},
			loadGoods() {
				if (this.uid == null || this.uid == '') {
					uni.redirectTo({
						url: "/pages/longin/longin"
					})
				} else {
					loadCartList(this.uid).then(data => {
						console.log("12345678", data.data);
						let shop = this.goodsProducts[0]; // 获取店铺对象
						data.data.forEach((item, index) => {
							let goods = {}; // 创建一个新的商品对象
							goods.img = item.specifications.specificationsImg;
							goods.title = item.books.booksName;
							goods.remark = item.specifications.specificationsName;
							goods.price = item.cartPrince;
							goods.number = item.carttNumber;
							goods.cartId = item.cartId;
							goods.checked = 1;
							shop.goods.push(goods); // 将新商品添加到 goods 数组
						})
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.shop-car {
		width: 750rpx;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		background: #F5F5F5;
	}
</style>